	/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package GUI.NVK;

import DAO.KhaNangCungUngDao;
import DAO.NhaCungCapDao;
import DAO.SanPhamDao;
import GUI.MainForm;
import POJO.KhaNangCungUng;
import POJO.NhaCungCap;
import POJO.SanPham;
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.beans.PropertyVetoException;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.DefaultComboBoxModel;
import javax.swing.DefaultListModel;
import javax.swing.JButton;
import javax.swing.JDesktopPane;
import javax.swing.JInternalFrame;
import javax.swing.JOptionPane;

/**
 *
 * @author Khoi
 */
public class frKhaNangCungUng extends javax.swing.JInternalFrame {

	/**
	 * Creates new form KhaNangCungCap
	 */
	private DefaultComboBoxModel cbModel;
	private DefaultComboBoxModel cbModel2;
	private DefaultListModel lbModel;

	private void loadCobo() {
		load = false;
		NhaCungCapDao dao = new NhaCungCapDao();

		ArrayList<NhaCungCap> ds = new ArrayList<NhaCungCap>();
		ds = dao.layDanhSach();
		for (int i = 0; i < ds.size(); i++) {
			this.cbModel.addElement(ds.get(i));
		}
		if (cbModel.getSize() > 0) {
			cbModel.setSelectedItem(cbModel.getElementAt(0));
		}
		load = true;
		hienThiSanPhamNhaCungCapKhongCungCap();
		hienThiSanPhamNhaCungCapCoCungCap();
	}

	private void loadCombo2(int MaNCC) {
		SanPhamDao dao = new SanPhamDao();

		ArrayList<SanPham> ds = new ArrayList<SanPham>();

		ds = dao.layDanhSachByMaNCCKhongCC(MaNCC);
		cbModel2.removeAllElements();// lam rong cb truoc khi them
		for (int i = 0; i < ds.size(); i++) {
			this.cbModel2.addElement(ds.get(i));
		}

	}

	private void hienThiSanPhamNhaCungCapKhongCungCap() {
		if (cbNhaCC.getSelectedObjects() != null) {
			int ma = ((NhaCungCap) cbNhaCC.getSelectedItem()).getMaNCC();
			loadCombo2(ma);
		}
	}

	private void hienThiSanPhamNhaCungCapCoCungCap() {
		if (cbNhaCC.getSelectedObjects() != null) {
			int ma = ((NhaCungCap) cbNhaCC.getSelectedItem()).getMaNCC();
			loadList(ma);
		}
	}

	private void loadList(int mncc) {
		if (mncc != 0) {
			SanPhamDao dao = new SanPhamDao();
			ArrayList<SanPham> ds = dao.layDanhSachByMaNhaCC(mncc);
			lbModel.removeAllElements();// lam rong danh sach truoc khi them

			for (int i = 0; i < ds.size(); i++) {
				this.lbModel.addElement(ds.get(i));
			}
		}
	}
	private boolean load;

	public frKhaNangCungUng() {

		initComponents();
		load = false;
		this.cbModel = new DefaultComboBoxModel();
		this.cbModel2 = new DefaultComboBoxModel();
		this.lbModel = new DefaultListModel();
		this.cbNhaCC.setModel(cbModel);
		this.cbSPTonTai.setModel(cbModel2);
		this.lsKhaNangCungUng.setModel(lbModel);
		loadCobo();

		// chon Nha cung cap dau tien trong Danh sach
		cbModel.setSelectedItem(cbModel.getElementAt(0));

		NhaCungCap tmp = (NhaCungCap) cbModel.getSelectedItem();
		// load danh sach theo doi tuong dau tien duoc chon 
		loadList(tmp.getMaNCC());


	}

	/**
	 * This method is called from within the constructor to initialize the
	 * form. WARNING: Do NOT modify this code. The content of this method is
	 * always regenerated by the Form Editor.
	 */
	@SuppressWarnings("unchecked")
       // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
       private void initComponents() {

              jPanel2 = new javax.swing.JPanel();
              cbNhaCC = new javax.swing.JComboBox();
              cbSPTonTai = new javax.swing.JComboBox();
              jScrollPane1 = new javax.swing.JScrollPane();
              lsKhaNangCungUng = new javax.swing.JList();
              jLabel1 = new javax.swing.JLabel();
              jLabel2 = new javax.swing.JLabel();
              jLabel3 = new javax.swing.JLabel();
              btThem = new javax.swing.JButton();
              btXoa = new javax.swing.JButton();
              btThemMoiSanPham = new javax.swing.JButton();
              jPanel4 = new javax.swing.JPanel();
              jLabel5 = new javax.swing.JLabel();

              setClosable(true);
              setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
              setIconifiable(true);
              setMaximizable(true);
              setResizable(true);

              cbNhaCC.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));
              cbNhaCC.setToolTipText("Tất cả các nhà cung cấp");
              cbNhaCC.addActionListener(new java.awt.event.ActionListener() {
                     public void actionPerformed(java.awt.event.ActionEvent evt) {
                            cbNhaCCActionPerformed(evt);
                     }
              });

              cbSPTonTai.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));
              cbSPTonTai.setToolTipText("Các mặt hàng mà nhà cung cấp hiện tai không cung cấp");

              lsKhaNangCungUng.setModel(new javax.swing.AbstractListModel() {
                     String[] strings = { "Item 1", "Item 2", "Item 3", "Item 4", "Item 5" };
                     public int getSize() { return strings.length; }
                     public Object getElementAt(int i) { return strings[i]; }
              });
              jScrollPane1.setViewportView(lsKhaNangCungUng);

              jLabel1.setText("Nhà cung cấp:");

              jLabel2.setText("Các sản phẩm tồn tại:");

              jLabel3.setText("Khả năng cung ứng:");

              btThem.setText("+");
              btThem.setToolTipText("Thêm khả năng cung ứng của nhà cung cấp");
              btThem.addActionListener(new java.awt.event.ActionListener() {
                     public void actionPerformed(java.awt.event.ActionEvent evt) {
                            btThemActionPerformed(evt);
                     }
              });

              btXoa.setText("-");
              btXoa.setToolTipText("Bớt khả năng cung úng của nhà cung cấp. Chỉ có thể xóa sản phẩm chưa được nhập");
              btXoa.addActionListener(new java.awt.event.ActionListener() {
                     public void actionPerformed(java.awt.event.ActionEvent evt) {
                            btXoaActionPerformed(evt);
                     }
              });

              btThemMoiSanPham.setText("Thêm sản phẩm");
              btThemMoiSanPham.setToolTipText("Gọi thêm mới sản phẩm");
              btThemMoiSanPham.addActionListener(new java.awt.event.ActionListener() {
                     public void actionPerformed(java.awt.event.ActionEvent evt) {
                            btThemMoiSanPhamActionPerformed(evt);
                     }
              });

              javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
              jPanel2.setLayout(jPanel2Layout);
              jPanel2Layout.setHorizontalGroup(
                     jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                     .addGroup(jPanel2Layout.createSequentialGroup()
                            .addContainerGap()
                            .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                   .addComponent(jLabel2)
                                   .addComponent(jLabel3)
                                   .addComponent(jLabel1)
                                   .addComponent(btThemMoiSanPham))
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                            .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                   .addComponent(cbSPTonTai, 0, 200, Short.MAX_VALUE)
                                   .addComponent(jScrollPane1)
                                   .addComponent(cbNhaCC, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                            .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                   .addComponent(btThem, javax.swing.GroupLayout.DEFAULT_SIZE, 56, Short.MAX_VALUE)
                                   .addComponent(btXoa, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                            .addContainerGap(97, Short.MAX_VALUE))
              );
              jPanel2Layout.setVerticalGroup(
                     jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                     .addGroup(jPanel2Layout.createSequentialGroup()
                            .addContainerGap()
                            .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                   .addComponent(jLabel1)
                                   .addComponent(cbNhaCC, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                            .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                   .addComponent(cbSPTonTai, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                   .addComponent(jLabel2)
                                   .addComponent(btThem))
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                            .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                   .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 246, Short.MAX_VALUE)
                                   .addGroup(jPanel2Layout.createSequentialGroup()
                                          .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                                 .addComponent(jLabel3)
                                                 .addComponent(btXoa))
                                          .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 196, Short.MAX_VALUE)
                                          .addComponent(btThemMoiSanPham)))
                            .addContainerGap())
              );

              jLabel5.setFont(new java.awt.Font("Tahoma", 0, 24)); // NOI18N
              jLabel5.setText("KHẢ NĂNG CUNG ỨNG CỦA NHÀ CUNG CẤP");

              javax.swing.GroupLayout jPanel4Layout = new javax.swing.GroupLayout(jPanel4);
              jPanel4.setLayout(jPanel4Layout);
              jPanel4Layout.setHorizontalGroup(
                     jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                     .addGroup(jPanel4Layout.createSequentialGroup()
                            .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(jLabel5)
                            .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
              );
              jPanel4Layout.setVerticalGroup(
                     jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                     .addGroup(jPanel4Layout.createSequentialGroup()
                            .addContainerGap()
                            .addComponent(jLabel5)
                            .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
              );

              javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
              getContentPane().setLayout(layout);
              layout.setHorizontalGroup(
                     layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                     .addGroup(layout.createSequentialGroup()
                            .addGap(0, 0, 0)
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                   .addComponent(jPanel4, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                   .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                            .addGap(0, 0, 0))
              );
              layout.setVerticalGroup(
                     layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                     .addGroup(layout.createSequentialGroup()
                            .addGap(0, 0, 0)
                            .addComponent(jPanel4, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addGap(0, 0, 0)
                            .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addGap(0, 0, 0))
              );

              pack();
       }// </editor-fold>//GEN-END:initComponents

       private void cbNhaCCActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cbNhaCCActionPerformed
		// TODO add your handling code here:
		if (load) {
			hienThiSanPhamNhaCungCapKhongCungCap();
			hienThiSanPhamNhaCungCapCoCungCap();

		}
       }//GEN-LAST:event_cbNhaCCActionPerformed

       private void btThemMoiSanPhamActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btThemMoiSanPhamActionPerformed
		// TODO add your handling code here:
		frQuanLySanPham fr = new frQuanLySanPham();

		fr.setVisible(true);
		MainForm.jDesktopPane1.add(fr);
		try {
			fr.setSelected(isSelected);
			fr.sanPhamMoi();
		} catch (PropertyVetoException ex) {
			Logger.getLogger(frKhaNangCungUng.class.getName()).log(Level.SEVERE, null, ex);
		}
       }//GEN-LAST:event_btThemMoiSanPhamActionPerformed

	private KhaNangCungUng getKNCU() {
		KhaNangCungUng tmp = new KhaNangCungUng(0, 0);
		if (cbNhaCC.getSelectedObjects() != null & cbSPTonTai.getSelectedObjects() != null) {
			NhaCungCap nc = (NhaCungCap) cbModel.getSelectedItem();
			SanPham sp = (SanPham) cbModel2.getSelectedItem();
			tmp.setMaNCC(nc.getMaNCC());
			tmp.setMaSP(sp.getMaSP());
		} else {
			if (cbNhaCC.getSelectedObjects() == null) {
				JOptionPane.showMessageDialog(this, "Chọn nhà cung cấp ", "Thông báo", JOptionPane.INFORMATION_MESSAGE);

			}
			if (lsKhaNangCungUng.getSelectedValue() == null) {
				JOptionPane.showMessageDialog(this, "Chọn sản phẩm bên danh sách sổ xuống ", "Thông báo", JOptionPane.INFORMATION_MESSAGE);

			}
		}
		return tmp;
	}

	private KhaNangCungUng getKNCUTonTai() {
		KhaNangCungUng tmp = new KhaNangCungUng(0, 0);
		if (cbNhaCC.getSelectedObjects() != null & lsKhaNangCungUng.getSelectedValue() != null) {
			NhaCungCap nc = (NhaCungCap) cbModel.getSelectedItem();
			SanPham sp = (SanPham) lsKhaNangCungUng.getSelectedValue();
			tmp.setMaNCC(nc.getMaNCC());
			tmp.setMaSP(sp.getMaSP());
		} else {
			if (cbNhaCC.getSelectedObjects() == null || lsKhaNangCungUng.getSelectedValue() == null) {
				JOptionPane.showMessageDialog(this, "Chọn sản phẩm bên danh sách số xuống ", "Thông báo lỗi", JOptionPane.ERROR_MESSAGE);

			}

		}
		return tmp;
	}

	private boolean coTheThem() {
		if (cbModel2.getSize() > 0) {
			return true;
		}
		return false;
	}
       private void btThemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btThemActionPerformed
		// TODO add your handling code here:
		if (!coTheThem()) {
			JOptionPane.showMessageDialog(this, "Không còn sản phẩm có khả năng cung ứng.", "Thông báo lỗi", JOptionPane.ERROR_MESSAGE);
			return;
		}

		KhaNangCungUng tmp = getKNCU();
		boolean kq = KhaNangCungUngDao.them(tmp);
		if (kq == true) {
			JOptionPane.showMessageDialog(this, "Thêm khả năng cung ứng thành công", "Thông báo", JOptionPane.INFORMATION_MESSAGE);
			//  LoadSanPham();
			hienThiSanPhamNhaCungCapKhongCungCap();
			hienThiSanPhamNhaCungCapCoCungCap();
		} else {
			JOptionPane.showMessageDialog(this, "Thêm khả năng cung ứng thất bại", "Cảnh báo!!!", JOptionPane.WARNING_MESSAGE);
		}

       }//GEN-LAST:event_btThemActionPerformed

       private void btXoaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btXoaActionPerformed
		// TODO add your handling code here:
		KhaNangCungUng tmp = getKNCUTonTai();
		if (tmp.getMaSP() == 0) {
			return;
		}
		boolean kq = KhaNangCungUngDao.xoa(tmp);
		if (kq == true) {
			JOptionPane.showMessageDialog(this, "Xóa khả năng cung ứng thành công", "Thông báo", JOptionPane.INFORMATION_MESSAGE);
			hienThiSanPhamNhaCungCapKhongCungCap();
			hienThiSanPhamNhaCungCapCoCungCap();
			//  LoadSanPham();
		} else {
			JOptionPane.showMessageDialog(this, "Xóa khả năng cung ứng thất bại", "Cảnh báo!!!", JOptionPane.WARNING_MESSAGE);
		}

       }//GEN-LAST:event_btXoaActionPerformed
       // Variables declaration - do not modify//GEN-BEGIN:variables
       private javax.swing.JButton btThem;
       private javax.swing.JButton btThemMoiSanPham;
       private javax.swing.JButton btXoa;
       private javax.swing.JComboBox cbNhaCC;
       private javax.swing.JComboBox cbSPTonTai;
       private javax.swing.JLabel jLabel1;
       private javax.swing.JLabel jLabel2;
       private javax.swing.JLabel jLabel3;
       private javax.swing.JLabel jLabel5;
       private javax.swing.JPanel jPanel2;
       private javax.swing.JPanel jPanel4;
       private javax.swing.JScrollPane jScrollPane1;
       private javax.swing.JList lsKhaNangCungUng;
       // End of variables declaration//GEN-END:variables
}
